
class Animation {
   
    private int index, currentFrameSet;
    private long timeLeft, timeChangeState;
    private byte mode;

    public int getFrameSet() {
        return (int)(Constants.ANIMATIONS[index][currentFrameSet] & 0xff);
    }

    private int getDelay() {
        return (int)((Constants.ANIMATIONS[index][currentFrameSet] >> 8) & 0xffff);
    }

    public void startAnimation(int index, byte mode) {
        this.index = index;
        this.mode = mode;
        currentFrameSet = 0;
        timeLeft = 0;
        timeChangeState = timeLeft + getDelay();
    }

    public void changeAnimation(int index) {
        if (currentFrameSet < Constants.ANIMATIONS[this.index].length) {
            this.index = index;
        }
    }

    public boolean updateAnimation(long dt) {
        timeLeft += dt;
        while (timeChangeState <= timeLeft) {
            currentFrameSet++;
            if (currentFrameSet >= Constants.ANIMATIONS[index].length) {
                if (mode == Constants.ANIMATION_PLAY_ONCE) return true;
                else currentFrameSet = 0;
            }
            timeChangeState += getDelay();
        }
        return false;
    }
}